/*
 * Copyright 2010 Pierre Heinzerling, Uwe Janner, Patrick Wright
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.upp.publinksky.model;

import com.google.appengine.api.datastore.Key;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.xml.bind.annotation.XmlRootElement;

/**
 */
@PersistenceCapable
@XmlRootElement(name = "invitation")
public class Invitation {
    public static final int STATUS_OPEN = 0;
    public static final int STATUS_ACCEPTED = 1;
    public static final int STATUS_DECLINED = 2;

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    @Persistent
    private Key theme;
    private int themeId;
    @Persistent
    private String fromEmail;
    @Persistent
    private String toEmail;

    /**
     * See constants at top of this class
     */
    @Persistent
    private int status;
    @Persistent
    private int lastRetrievedThemeCounter;

    public Invitation() {
    }

    public Invitation(final Key key, final Key theme, final String fromEmail, final String toEmail, final int status) {
        this.key = key;
        this.theme = theme;
        this.fromEmail = fromEmail;
        this.toEmail = toEmail;
        this.status = status;
    }

    public Key getKey() {
        return key;
    }

    public void setKey(final Key key) {
        this.key = key;
    }


    public String getFromEmail() {
        return fromEmail;
    }

    public void setFromEmail(final String fromEmail) {
        this.fromEmail = fromEmail;
    }

    public String getToEmail() {
        return toEmail;
    }

    public void setToEmail(final String toEmail) {
        this.toEmail = toEmail;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(final int status) {
        this.status = status;
    }

    public Key getTheme() {
        return theme;
    }

    public void setTheme(final Key theme) {
        this.theme = theme;
    }

    public int getThemeId() {
        return themeId;
    }

    public void setThemeId(final int themeId) {
        this.themeId = themeId;
    }

    public void setLastRetrievedThemeCounter(final int lastRetrievedThemeCounter) {
        this.lastRetrievedThemeCounter = lastRetrievedThemeCounter;
    }

    public int getLastRetrievedThemeCounter() {
        return lastRetrievedThemeCounter;
    }

    public boolean isDeclined() {
        return status == STATUS_DECLINED;
    }

    public boolean isInvitee(final String email) {
        return toEmail.equals(email);
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (!(o instanceof Invitation)) return false;

        final Invitation that = (Invitation) o;

        if (lastRetrievedThemeCounter != that.lastRetrievedThemeCounter) return false;
        if (status != that.status) return false;
        if (fromEmail != null ? !fromEmail.equals(that.fromEmail) : that.fromEmail != null) return false;
        if (key != null ? !key.equals(that.key) : that.key != null) return false;
        if (theme != null ? !theme.equals(that.theme) : that.theme != null) return false;
        if (toEmail != null ? !toEmail.equals(that.toEmail) : that.toEmail != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = key != null ? key.hashCode() : 0;
        result = 31 * result + (theme != null ? theme.hashCode() : 0);
        result = 31 * result + (fromEmail != null ? fromEmail.hashCode() : 0);
        result = 31 * result + (toEmail != null ? toEmail.hashCode() : 0);
        result = 31 * result + status;
        result = 31 * result + lastRetrievedThemeCounter;
        return result;
    }
}
